package fons.model.ontology.language;

import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import fons.model.ontology.Dimension;

public class DimensionSentenceElement extends SentenceElementMixin {
	private List<ModifierSentenceElement> modifiers = Lists.newLinkedList();
	
	public DimensionSentenceElement(SentenceElement other) {
		super(other);
	}
	
	public DimensionSentenceElement(Set<Dimension> dimensions) {
		super(new ImplicitSentenceElement(Sets.newHashSet(dimensions)));
	}

	public void setDimensions (Set<Dimension> dimensions) {
		if (!this.holds(Set.class)) throw new ClassCastException();
		
		@SuppressWarnings("unchecked")
		Set<Dimension> container = (Set<Dimension>) this.get(Set.class);
		container.clear();
		container.addAll(dimensions);
	}
	
	@SuppressWarnings("unchecked")
	public Set<Dimension> getDimensions() {
		Set<Dimension> dimensions;
		if (this.holds(Dimension.class)) {
			dimensions = Sets.newHashSet(get(Dimension.class));
		} else {
			dimensions = Sets.newHashSet((Set<Dimension>) this.get(Set.class));
		}
		
		for (ModifierSentenceElement modifier: modifiers) {
			dimensions = modifier.get().modify(dimensions, modifier);
		}
		return dimensions;
	}
	
	public void addModifier(ModifierSentenceElement modifier) {
		modifiers.add(modifier);
		Collections.sort(modifiers);
	}
}
